package com.xxq.semester.controller.classmgmt;


import com.xxq.semester.entity.classmgmt.Classes;
import com.xxq.semester.entity.utils.ResponseEntity;
import com.xxq.semester.mapper.ClassesMapper;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author saki
 * @since 2025-07-13
 */
@RestController
@Tag(name = "班级管理")
@Transactional
@RequestMapping("/api/classes")
public class ClassesController {
    @Autowired
    private ClassesMapper classMapper;
    @Operation(summary = "获取所有班级信息")
    @GetMapping("/getAllClasses")
    public ResponseEntity<List<Classes>> getAllClasses() {
        return ResponseEntity.success(classMapper.getAllClasses());
    }
    @Operation(summary = "创建班级")
    @PostMapping("/createClass")
    public ResponseEntity<String> createClass(@RequestBody Classes classes) {
        return classMapper.createClass(classes) ? ResponseEntity.success("创建成功") : ResponseEntity.fail("创建失败");
    }
    @Operation(summary = "更新班级信息")
    @PostMapping("/updateClassById")
    public ResponseEntity<String> updateClassById(@RequestBody Classes classes) {
        return classMapper.updateClassById(classes) ? ResponseEntity.success("更新成功") : ResponseEntity.fail("更新失败");
    }
    @Operation(summary = "删除班级")
    @PostMapping("/deleteClassById")
    public ResponseEntity<String> deleteClassById(Integer classId) {
        return classMapper.deleteClassById(classId) ? ResponseEntity.success("删除成功") : ResponseEntity.fail("删除失败");
    }
    @Operation(summary = "获取班级信息")
    @GetMapping("/getClassById")
    public ResponseEntity<Classes> getClassById(@RequestParam Integer classId) {
        return ResponseEntity.success(classMapper.getClassById(classId));
    }
    @Operation(summary = "通过班级Id获取教师Id")
    @GetMapping("/getTeacherIdByClassId")
    public ResponseEntity<Integer> getTeacherIdByClassId(@RequestParam Integer classId) {
        if(classMapper.getTeacherIdByClassId(classId) != null){
            return ResponseEntity.success(classMapper.getTeacherIdByClassId(classId));
        }
        return ResponseEntity.fail("获取教师Id失败");
    }
}
